$(function () {
    //rem适配
    setRem();
    function setRem() {
        //获得屏幕大小
        let htmlwidth = document.documentElement.clientWidth || document.body.clientWidth;  //浏览器兼容

        //获得html DOM元素
        let htmlDom = document.getElementsByTagName('html')[0];

        //以414尺寸为例
        htmlDom.style.fontSize = (htmlwidth / 414) * 10 + 'px';
    }
    window.onresize = setRem;

    const main_id = getUrlParam('main_id');
    if (!main_id) return;
    $("#mainId").html(`main_id=${main_id}&action=waybill`)

    //实例化clipboard
    var clipboard = new ClipboardJS('.fixedBtn');

    //复制成功的响应事件【按F12控制台可见】
    clipboard.on('success', function (e) {
        //复制成功 开始跳转
        var u = navigator.userAgent;
        var isAndroid =  u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
        var isIos = u.indexOf('iPhone') > -1;
        var androidUrl = encodeURIComponent(`zhonghao://appdown?main_id=${main_id}&action=waybill`);
        var url =  `https://www.zhgylgl.com/appdown?main_id=${main_id}&action=waybill&url=${androidUrl}`;
        if(isIos){
            //iphone
            location.href = url;
        }
        else if(u.toLocaleLowerCase().indexOf('micromessenger') !== -1){
            //微信环境
            $(".fixedBg").show();
            $(".fixedBg").off('click').on("click",function(){
                $(".fixedBg").hide();
            })
        }else if(isAndroid){
            location.href = url;
        }
    });

    //获取字段
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }

    //加密方法
    function decodeData() {
        const userAgent = navigator.userAgent;
        if (!userAgent) return {};
        const salt = 'emhhHaC';
        const timeStamp = Date.now() + '';
        const sign = '01' + md5(timeStamp + salt)
        return {
            sign,
            timeStamp
        }
    }
    //使用JavaScript内置的Date对象进行时间戳转日期的实现
    function timestampToDate(timestamp) {
        // 此处时间戳以毫秒为单位
        let date = new Date(parseInt(timestamp) * 1000);
        let Year = date.getFullYear();
        let Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
        let Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let Hour = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
        let Minute = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
        let Sechond = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        let  GMT =  Year + '-' + Moth + '-' + Day + '   '+ Hour +':'+ Minute  + ':' + Sechond;
        return GMT
    }
    const signData = decodeData();

    $.ajax({
        //请求的方式：get/post
        url: ' https://api.zhgylgl.com/api/user_order/getBillDetail',
        type: 'get',
        headers: {
            'os': 'web',
            'x-time': signData.timeStamp,
            'x-sign': signData.sign,

        },//请求头
        //这次请求要携带的数据（不需要参数可以省略）
        data: {
            main_id,
        },
        //请求成功之后的回调函数
        success: function (res) {
            if (typeof res == 'string') {
                res = JSON.parse(res)
            }
            if (res.code != 200) return;
            var obj = res.data;
            for (var key in obj) {
                var data = obj[key];
                //特殊处理字段
                if (key == 'chechang') {
                    data = data ? data : '不限车长'
                }
                if (key == 'remark') {
                    data = data ? data : '无备注'
                }
                if(key == 'last_xh_time'){
                    data = timestampToDate(data)
                }
                $(`[detail=${key}]`).html(data)
            }
        }
    })


})

